Skip to content
PostTheory Of Computation / Lecture

03-TOC-Finite Automata

2024-10-03
Back to Blog

TOC Finite Automata ​

The computation power of this model is rather weak. Because it can only use a limited (finite) memory. But it is also useful in some applications.

DFA ​

+ Deterministic finite automata $DFA$

A deterministic finite automata (DFA) is a 5-tuple (Q,Σ,δ,q0,F) , where

  • Q is a finite set called states,
  • Σ is a finite set called alphabet,
  • δ:Q×Σ→Q is the transition function,
  • q0∈Q is the start state (also called initial state), and
  • F⊆Q is the set of accept states (final states).

The transition function δ can also be represented as a transition table. The table has |Q| rows and |Σ| columns. The value in each entry is a state. δ is a total function. So, every entry has a value. The input of a DFA is a string in the language Σ∗. The output of a DFA is only accept or reject.

Suppose w=w1w2⋯wn is a string in Σ∗. The DFA M accepts w if and only if:

δ(q0,w1)=qs1δ(qs1,w2)=qs2⋮δ(qsn−1,wn)=qsn∈F

Each qsi is a state of M. Each δ(qsi,wi+1)=qsi+1 is a transition. qsn is a final state in F.

The sequence of transitions can also be expressed as:

δ(⋯δ(δ(q0,w1),w2)⋯,wn)∈F

Remember, a DFA can be represented as a state graph. The features of the graph are listed:

  • A state is in a circle.
  • A transition is an arc from the current state to the next state.
  • The input symbol required by a transition is the label of the arc.
  • The initial state is pointed by an arrow.
  • Each final state is double circled.

Example

Let M1=({q0,q1},{0,1},δ,q0,{q1}) be a DFA, where the transition function δ is:

01
q0q0q1
q1q0q1
L(M1)={w∈{0,1}∗∣w end with 1’s}.

If A is the set of all strings that M accepts, then:

  • A is the language of machine M;
  • L(M)=A;
  • M recognizes A.

For example:

L(M1)={w∣w ends with a 1}.

A language is regular if it is recognized by some DFA (Deterministic Finite Automaton).

NFA ​

+ Nondeterministic finite automata $NFA$.

An NFA (Nondeterministic Finite Automaton) is a 5-tuple (Q,Σ,δ,q0,F), where:

  • Q is the set of states;
  • Σ is the alphabet;
  • δ:Q×Σ→P(Q) is the transition function;
  • q0∈Q is the start state; and
  • F⊆Q is the set of final states.

Suppose M is an NFA to recognize the language A.

  • If w is a string in the language A:

    • Then there exists a "way" to reach the final state;
    • It's also possible to reach a non-final state.
  • But if w is not a string in the language A:

    • Then there is no way to reach the final state.

Please pay attention to the quantifiers.

Different between DFA and NFA ​

The difference between a DFA and an NFA is the transition function δ.

  • DFA: δ:Q×Σ→Q
  • NFA: δ:Q×Σ→P(Q)
    • P(Q) is the power set of Q.

This difference allows:

  • One state to transition to zero, one, or multiple states by taking one input symbol.

ε−Moves ​

Automatas can be further upgraded by a special transition.

+ $\epsilon-Move$

An ϵ−Move is a transition from one state to another state by taking no input symbol.

Such automata is defined.

+ NFA with $\epsilon-Move$

An NFA with ε-moves is the same as an NFA (without ε-moves) except the transition function:

δ:Q×Σ∪{ε}→P(Q)

Later, we will see that ε-moves allow us to recursively construct NFAs, which is... elegant.

Equivalence of DFA and NFA ​

Power ​

Let M1 and M2 be two computation models. M1 is as powerful as M2 if:

  • For all languages L, L is recognized by some machines in M1 if and only if it is recognized by some machines in M2.

M1 is more powerful than M2 if there is a language L which can be recognized by some machines in M1 but no machine in M2.

Note: A computation model is a set of all machines of the same type.

+ Theorem

DFA, NFA, and NFA with ε-moves are of the same computation power.

For convenience, we denote the ordering on the power by =pow, <pow, ≤pow, etc. Trivially,

+ Lemma

DFA≤powNFA≤powNFAε

Next, we only need to show NFAε≤powDFA.

It seems the content repeats the explanation for converting NFAε to a DFA and introduces the concept of ε-closure more formally. Here's a summary in Markdown format for your reference:

The Intuition of the Proof:

  • For every NFAε, we can construct a DFA that recognizes the same language.
  • This is equivalent to the process of converting an NFA to an equivalent DFA.

+ Def: $\varepsilon$-closure

Let N=(Q,Σ∪{ε},δ,q0,F) be an NFAε.
The ε-closure of a state q, denoted E(q), is defined as:

E(q)={q}∪δ(q,ε)∪δ(δ(q,ε),ε)∪…
  • E(q) represents the set of all states that can be reached starting from q only using ε-transitions.
  • This closure includes q itself and any other states reachable via one or more ε-moves.
  • Alternative Name: The ε-closure is sometimes referred to as the Kleene Star or Kleene Closure.

+ Def: $\upvarepsilon-Closure$ of a Set of States

Let R be a set of states for a given NFA. The ε-closure of R, denoted E(R), is:

E(R)=⋃q∈RE(q)

This means the ε-closure of R is the union of the ε-closures of all individual states in R. E(q) is the set of states reachable from q using only ε-transitions.

+ Def: **$a$-Move of a Set of States:**

Let R be a set of states, and let a be a symbol for a given NFA. The a-move of R, denoted δ(R,a), is:

δ(R,a)=⋃q∈RE(δ(q,a))

This describes all the states reachable by:

  1. First making an a-transition from any state q in R, and
  2. Then taking all ε-transitions from those resulting states.

NOTE: δ(q,a) gives the set of states reachable by consuming the symbol a.

Construction Algorithm ​

Let N=(Q,Σ,δ,q0,F) be an NFAε. We construct a DFA D=(Q′,Σ,δ′,q0′,F′), where:

  • Q′=P(Q)
  • δ′:P(Q)×Σ→P(Q) such that:δ′(R,a)={q∈Q∣∃r∈R,q∈E(δ(r,a))}
  • q0′=E(q0)
  • F′={R∈Q′∣R∩F≠∅}

States (Q′):

  • Q′=P(Q), the power set of Q.
  • Each state in D is a subset of the states in N.

Transition Function (δ′):

δ′:P(Q)×Σ→P(Q), defined as: $$\delta'(R, a) = { q \in Q \mid \exists r \in R, q \in E(\delta(r, a)) }$$

  • For each state R⊆Q and symbol a∈Σ:
  • Find all states q reachable by an a-move from some r∈R, considering ε-closures.

Start State (q0′):

  • q0′=E(q0), the ε-closure of the initial state q0 of N.

Final States (F′):

  • F′={R∈Q′∣R∩F≠∅}.
  • A subset R of states is a final state in D if it includes any final state of N.

Ensures D captures all possible behaviors of N, including ε-transitions. Uses the power set of Q as the state space to simulate nondeterminism deterministically.


Pseudocode ​

Algorithm 2 Convert NFAε_\varepsilon to DFA

1:Input: An NFAε_\varepsilon N=(Q,Σ,δ,q0,F)N = (Q, \Sigma, \delta, q_0, F)

2:Output: A DFA D=(Q′,Σ,δ′,q0′,F′)D = (Q', \Sigma, \delta', q_0', F')

3:Q′←{E(q0)}Q' \gets \{E(q_0)\}, E(q0)E(q_0) is unmarked, and δ′←∅\delta' \gets \emptyset

4:while there exists an unmarked state R∈Q′R \in Q' do

5:Mark RR

6:for each symbol a∈Σa \in \Sigma do

7:U←E(δ(R,a))U \gets E(\delta(R, a))

8:if U∉Q′U \notin Q' then

9:Add UU to Q′Q'

10:end if

11:Add δ′(R,a)=U\delta'(R, a) = U to δ′\delta'

12:end for

13:end while

14:q0′←E(q0)q_0' \gets E(q_0)

15:F′←{R∣R has a final state in F}F' \gets \{R \mid R \text{ has a final state in } F\}

16:

17:return D←(Q′,Σ,δ′,q0′,F′)D \gets (Q', \Sigma, \delta', q_0', F')

Example ​

Let’s try to convert this NFA to DFA.

Initially ​

Suppose the unmarked states are red.

Q′={E(q0)={q0,q2}},δ′={}

Step 1 ​

E(δ({q0,q2},0))=E({q0})={q0,q2}E(δ({q0,q2},1))=E({q1})={q1}

Step 2 ​

E(δ({q1},0))=E({q1,q2})={q1,q2}E(δ({q1},1))=E({q2})={q2}

Step 3 ​

E(δ({q1,q2},0))=E({q0,q1,q2})={q0,q1,q2}E(δ({q1,q2},1))=E({q2})={q2}

Step 4 ​

E(δ({q2},0))=E({q0})={q0,q2}E(δ({q2},1))=E({})={}

Step 5 ​

E(δ({q0,q1,q2},0))=E({q0,q1,q2})={q0,q1,q2}E(δ({q0,q1,q2},1))=E({q1,q2})={q1,q2}

E(δ({},0))=E({})={}E(δ({},1))=E({})={}

Finally ​

{q0,q2} and {q0,q1,q2} are final states.

Rename the states to make the DFA more readable and beautiful: q0′,q1′,q2′,q3′,q4′,q5′.

q5′ is a trap state because it has no valid transitions.